package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询菜品总览，起售和停售菜品数量
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        // 分别查询起售和停售菜品数量
        Dish dish = Dish.builder().status(StatusConstant.ENABLE).build();
        Integer sold = dishMapper.selectCountByStatus(dish);

        dish.setStatus(StatusConstant.DISABLE);
        Integer discontinued = dishMapper.selectCountByStatus(dish);

        // 封装VO返回结果
        return DishOverViewVO
                .builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询套餐总览，起售和停售套餐数量
     *
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        // 分别查询起售和停售套餐数量
        Setmeal setmeal = Setmeal.builder().status(StatusConstant.ENABLE).build();
        Integer sold = setmealMapper.selectCountByStatus(setmeal);

        setmeal.setStatus(StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.selectCountByStatus(setmeal);

        // 封装VO返回结果
        return SetmealOverViewVO
                .builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询各个状态订单数量
     *
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        Orders orders = Orders.builder().status(null).build();
        Integer allOrders = ordersMapper.getCountByStatus(orders);

        orders.setStatus(Orders.PAID);
        Integer waitingOrders = ordersMapper.getCountByStatus(orders);

        orders.setStatus(Orders.CONFIRMED);
        Integer deliveredOrders = ordersMapper.getCountByStatus(orders);

        orders.setStatus(Orders.COMPLETED);
        Integer completedOrders = ordersMapper.getCountByStatus(orders);

        orders.setStatus(Orders.CANCELLED);
        Integer cancelledOrders = ordersMapper.getCountByStatus(orders);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .build();
    }

    /**
     * 查询今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO businessData(LocalDate begin, LocalDate end) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */

        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);

        //查询总订单数
        Integer totalOrderCount = ordersMapper.countByMap(map);

        map.put("status", Orders.COMPLETED);
        //营业额
        Double turnover = ordersMapper.sumByMap(map);
        turnover = turnover == null? 0.0 : turnover;

        //有效订单数
        Integer validOrderCount = ordersMapper.countByMap(map);

        Double unitPrice = 0.0;

        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount != 0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }

        //新增用户数
        Integer newUsers = userMapper.countByMap(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }
}
